﻿using System.IO;

namespace ZPeng.CSharpPro.Model.Excel
{
    public class FileCache
    {
        private readonly static string FilePath = "FileCache.dat";

        public FileCache()
        {
        }

        // 写入数据
        public void WriteData(string data)
        {
            // 确保线程安全
            lock (this)
            {
                using (StreamWriter sw = File.AppendText(FilePath))
                {
                    sw.WriteLine(data);
                }
            }
        }

        // 读取数据
        public List<string> ReadData()
        {
            List<string> dataItems = new List<string>();

            // 确保线程安全
            lock (this)
            {
                // 读取所有行
                string[] lines = File.ReadAllLines(FilePath);
                if (lines.Length > 0)
                {
                    dataItems.AddRange(lines);

                    // 清空文件
                    File.WriteAllText(FilePath, string.Empty);
                }
            }

            return dataItems;
        }
    }
}
